Ext.define('StaffServer.controller.PracticeInfoController', {
	extend : 'Ext.app.Controller',

	models: ['TeacherModel','PracticeInfoModel'],
	stores: ['TeacherStore','PracticeInfoStore'],
	views:  [
	         'practiceinfo.PracticeInfoMngr',
	         'practiceinfo.PracticeInfoSearchPanel',
	         'practiceinfo.PracticeInfoDetailGrid',
	        
	         'practiceinfo.PracticeInfoDetailWindow',
	         'practiceinfo.PracticeInfoDetailForm',
	         'practiceinfo.PracticeInfoExcelWindow',
	         'practiceinfo.PracticeInfoExcelForm'
	         ],
	
	tabView: 'StaffServer.view.practiceinfo.PracticeInfoMngr',
	practiceInfoDetailWindow:null,
	practiceInfoDetailForm:null,

	practiceInfoForm:null,
	practiceInfoExcelWindow:null,
	practiceInfoExcelForm:null,
	submitFlag: null,
	retireId: null,
	staffRecord: null,
	refs : [ {
		ref : 'practiceInfoSearchPanel',
		selector : 'practiceInfoSearchPanel'
	}, {
		ref : 'practiceInfoDetailGrid',
		selector : 'practiceInfoDetailGrid'
	}],

	// 初始化执行语句
	init : function() {                  
		
		practiceInfoDetailWindow= Ext.widget("practiceInfoDetailWindow");
		practiceInfoDetailForm = Ext.ComponentQuery.query('practiceInfoDetailForm')[0];
	    practiceInfoExcelWindow = Ext.widget("practiceInfoExcelWindow");
	    practiceInfoExcelForm = Ext.ComponentQuery.query('practiceInfoExcelForm')[0];
		
		// 控制事件
		this.control({
			'practiceInfoSearchPanel button[action=search]' : {
				click : this.search
			},
			'practiceInfoSearchPanel button[action=reset]' : {
				click : this.reset
			},
			
			'practiceInfoDetailGrid button[action=savePracticeInfo]' : {
				click : this.savePracticeInfo
			},
			'practiceInfoDetailWindow button[action=close]' : {
				click : this.closeDetailWindow
			},
			'practiceInfoDetailWindow button[action=submit]' : {
				click : this.submitDetialWindow
			},
			'practiceInfoDetailGrid button[action=deletePracticeInfo]' : {
				click : this.deletePracticeInfo
			},
			'practiceInfoDetailGrid button[action=updatePracticeInfo]' : {
				click : this.updatePracticeInfo
			},
			
			'practiceInfoDetailGrid button[action=exportExcel]' : {
				click : this.exportExcel
			},
			'practiceInfoDetailGrid button[action=importExcel]' : {
				click : this.importExcel
			},
			'practiceInfoExcelWindow button[action=submit]' : {
				click : this.submitExcelWindow
			},
			'practiceInfoExcelWindow button[action=close]' : {
				click : this.closeExcelWindow
			}
		});
	},
	
	//提交表单
	submitExcelWindow: function(){
		if (!practiceInfoExcelForm.form.isValid()) {
            return;
        }
		var $this = this;
		var values = practiceInfoExcelForm.getValues();
		practiceInfoExcelForm.getForm().submit({
			waitTitle: '系统提示',   
  			waitMsg: '正在处理,请稍候', 
			clientValidation: true,
		    url: 'practiceinfo/importExcel',
		    params: {
		    	values: values
		    },
		    success: function(form, action) {
		    	Ext.Msg.alert('提示', "操作成功", function(){
		    		$this.closeExcelWindow();
		    		$this.search();
		    	});
		    },
		    failure: function(form, action) {
		    	var resp = Ext.JSON.decode(response.responseText);
                        Ext.Msg.alert('提示', resp.error);
		    }
		});
	},
	//关闭窗口
	closeExcelWindow: function(){
		practiceInfoExcelForm.getForm().reset();
		practiceInfoExcelWindow.hide();
	},
	
	//导入
	importExcel: function(){
		practiceInfoExcelWindow.setTitle("导入文件");
		practiceInfoExcelWindow.show();
	},
	
	exportExcel: function(){
		var searchPanel = this.getPracticeInfoSearchPanel();
		var searchParams = searchPanel.getForm().getFieldValues();
		var jsonFilter = Ext.JSON.encode(searchParams);
		window.location.href = 'practiceinfo/exportPracticeInfoExcel?jsonFilter=' + jsonFilter;
	},
	
	
	
	//更新
	updatePracticeInfo: function(){
		var record = this.getPracticeInfoDetailGrid().getSelectionModel().getLastSelected();
        if (Ext.isEmpty(record)) {
            Ext.Msg.alert('提示', '请先选择要操作的记录!');
            return;
        }
        practiceInfoDetailForm.loadRecord(record);
        submitFlag = "update";
        practiceInfoDetailWindow.setTitle("修改教师企业培训信息");
        practiceInfoDetailWindow.show();
	},
	
	closeDetailWindow:function(){
		practiceInfoDetailForm.getForm().reset();
		practiceInfoDetailWindow.hide();
		
	},
	
	
	//删除
	deletePracticeInfo: function(){
		var $this = this;
		var record = this.getPracticeInfoDetailGrid().getSelectionModel().getLastSelected();
        if (Ext.isEmpty(record)) {
            Ext.Msg.alert('提示', '请先选择要操作的记录!');
            return;
        }
        Ext.MessageBox.confirm('请确认', '确认删除吗?', function(btn, text){
            if (btn == 'yes') {
                showWaitMsg(); 
                Ext.Ajax.request({
                    url: 'practiceinfo/deletePracticeInfo',
                    params: {
                    	sn: record.get("sn")
                    },
                    success: function(response){
                    	var resp = Ext.JSON.decode(response.responseText);
                        if (resp.success) {
                            Ext.Msg.alert('提示', '操作成功', function(){
                            	$this.getPracticeInfoDetailGrid().getStore().reload();
                            });
                        }
                        else {
                            Ext.Msg.alert('提示', resp.error);
                        }
                    },
                    failure: function(response){
                    	var resp = Ext.JSON.decode(response.responseText);
                        Ext.Msg.alert('提示', resp.error);
                    }
                });
            }
        });
	},
	
	
	//提交表单
	submitDetialWindow: function(){
		if (!practiceInfoDetailForm.form.isValid()) {
            return;
        }
		var $this = this;
		var values = practiceInfoDetailForm.getValues();
		practiceInfoDetailForm.getForm().submit({
			waitTitle: '系统提示',   
  			waitMsg: '正在处理,请稍候', 
			clientValidation: true,
		 url: submitFlag == 'save' ? 'practiceinfo/savePracticeInfo' : 'practiceinfo/updatePracticeInfo',
		//	 url: submitFlag == 'save' ? 'degreeStudy/saveDegreeStudy' : 'degreeStudy/updateDegreeStudy',
			params: {
		    	values: values
		    },
		    success: function(form, action) {
		    	Ext.Msg.alert('提示', "操作成功", function(){
		    		$this.closeDetailWindow();
		    		$this.getPracticeInfoDetailGrid().getStore().reload();
		    	});
		    },
		    failure: function(form, action) {
		    	var resp = Ext.JSON.decode(action.response.responseText);
		    	Ext.Msg.alert('提示', resp.error);
		    }
		});
	},
	
	//新增
	savePracticeInfo: function(){
		submitFlag = "save";
		practiceInfoDetailWindow.setTitle("新增教师企业实践信息");
		practiceInfoDetailWindow.show();
	},
	
	
	
	//searchPanel查询
	search : function() {
		var searchPanel = this.getPracticeInfoSearchPanel();
		var grid = this.getPracticeInfoDetailGrid();
		grid.store.on('beforeload', function(store){
			var searchParams = searchPanel.getForm().getFieldValues();
			Ext.apply(store.proxy.extraParams, searchParams);
        });
		Ext.ComponentQuery.query('practiceInfoDetailGrid > pagingtoolbar')[0].moveFirst();
	},
	
	
	//searchPanel重置
	reset: function(){
		var searchPanel = this.getPracticeInfoSearchPanel();
		searchPanel.getForm().reset();
	}
	

});